package com.example.demo.controller;

import com.example.demo.entity.Folder;
import com.example.demo.exception.BusyException;
import com.example.demo.exception.FolderException;
import com.example.demo.exception.file.InvalidPathException;
import com.example.demo.service.FolderService;
import com.example.demo.utils.FolderUtils;
import com.example.demo.utils.Message;
import com.jcraft.jsch.SftpException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("folder")
public class FolderController {

    @Resource
    FolderService folderService;

    /**
     * 新建文件夹
     *
     * @param folder 文件夹路径 缺少“/root/user/用户名/”路径
     * @return 是否创建成功
     */
    @RequestMapping(value = "createFolder", method = RequestMethod.POST)
    public Message createFolder(@RequestBody Folder folder) {
        String msg = "创建文件夹成功";
        String path = folder.getPath();
        try {
            //这里是在file默认文件夹下创建自己的文件夹
            folderService.creatFolder(path);
            return Message.createSuc(msg);
        } catch (IOException | BusyException e) {
            e.printStackTrace();
            msg = "系统繁忙，创建文件夹失败";
            return Message.createErr(msg);
        } catch (FolderException e) {
            msg = "该文件夹已存在，请勿重复创建";
            return Message.createErr(msg);
        } catch (SftpException e) {
            e.printStackTrace();
            msg = "sftp连接失败";
            return Message.createErr(msg);
        }
    }

    /**
     * 上传文件夹
     *
     * @param path   文件夹路径
     * @param folder 文件夹
     * @return 是否上传成功
     */
    @ResponseBody
    @RequestMapping(value = "uploadFolder", method = RequestMethod.POST)
    public Message uploadFolder(@RequestParam("path") String path, @RequestParam("folder") MultipartFile[] folder) {
        String msg = "上传文件夹成功";
//        String path=folder.getPath();
        try {
            //上传文件夹到‘我的文件夹’路径下,这里前端应该以“我的文件夹”开头
            folderService.uploadFolder(path, folder);
            return Message.createSuc(msg);
        } catch (IOException e) {
            e.printStackTrace();
            msg = "系统繁忙1，上传文件夹失败";
            return Message.createErr(msg);
        } catch (BusyException e) {
            msg = "系统繁忙2，上传文件夹失败";
            return Message.createErr(msg);
        } catch (SftpException e) {
            e.printStackTrace();
            msg = "连接sftp失败";
            return Message.createErr(msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg = "系统繁忙3，上传文件夹失败";
            return Message.createErr(msg);
        }
    }

    /**
     * 获取用户的所有文件夹目录
     *
     * @return 用户的所有文件夹目录
     */
    @RequestMapping(value = "getFolders", method = RequestMethod.GET)
    public Message getFolders() {
        Object msg;
        try {
            //在file默认路径下寻找文件夹目录
            msg = folderService.getFolders(FolderUtils.file);
            return Message.createSuc(msg);
        } catch (SftpException e) {
            e.printStackTrace();
            msg = "sft连接失败";
            return Message.createErr((String) msg);
        }

    }

    /**
     * 删除文件/夹
     * @param path 被移动的文件夹的路径（以"我的文件夹"开头）
     * @return 是否删除成功
     */
    @RequestMapping(value = "deleteFolder", method = RequestMethod.POST)
    public Message deleteFolder(@RequestParam("path") String path) {
        String msg = "删除文件夹成功，可在回收站中查看";
        try {
            folderService.deleteFolders(path, FolderUtils.file, FolderUtils.trash);
            return Message.createSuc(msg);
        } catch (SftpException e) {
            e.printStackTrace();
            msg = "ssh连接失败";
            return Message.createErr(msg);
        } catch (InvalidPathException e) {
            msg="原文件夹路径有误，删除文件夹失败";
            return Message.createErr(msg);
        }
    }
    /**
     * 恢复文件/夹
     * @param path 被移到的文件夹路径（
     * @return 是否恢复成功
     */
    @RequestMapping(value = "renewFolder", method = RequestMethod.POST)
    public Message renewFolder(@RequestParam(value = "path",required = false) String path) {
        String msg = "恢复文件夹成功，可在我的文件中查看";
        try {
            //folderService.deleteFolders(path, FolderUtils.trash, FolderUtils.file);
            folderService.renewFolders(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException | SftpException e) {
            msg="原文件夹路径有误，恢复文件夹失败";
            return Message.createErr(msg);
        }
    }

    /**
     * 永久删除文件/夹
     * @param path 被删除文件/夹路径
     * @return 是否删除成功
     */
    @RequestMapping(value="deleteFolderForever",method = RequestMethod.DELETE)
    public Message deleteFolderForever(@RequestParam(value = "path",required = false) String path){
        String msg="删除文件/夹成功";
        try{
            folderService.deleteFolderForever(path);
            return Message.createSuc(msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg="系统繁忙，删除文件/夹失败";
            return Message.createErr(msg);
        }

    }
}
